
vec2 fixuv(in vec2 uv){
  vec2 p=5.*(2.*uv-iResolution.xy)/min(iResolution.x,iResolution.y);
  return p;
}

vec3 GridPanel(in vec2 p){
  vec3 col=vec3(0);
  vec2 fraction=1.-2.*abs(fract(p)-.5);
  // 网格线
  col = mix(col,vec3(1.),smoothstep(fraction.x*0.9,fraction.x,2.*fwidth(fraction.x)));
  col = mix(col,vec3(1.),smoothstep(fraction.y*0.9,fraction.y,2.*fwidth(fraction.y)));
  // 十字线
  col = mix(col,vec3(1.,0.,0.),smoothstep(abs(p.x)*0.9,abs(p.x),2.*fwidth(p.x)));
  col = mix(col,vec3(0.,1.,0.),smoothstep(abs(p.y)*0.9,abs(p.y),2.*fwidth(p.y)));

  return col;
}

float segment(in vec2 p,in vec2 sp,in vec2 ep,in float w){
  vec2 end=sp-ep;//结束点指向起始点的向量
  vec2 start=-end;//ba //起始点指向结束点的向量
  vec2 fend=p-ep;//结束点指向画布中任意点的向量
  vec2 fstart=p-sp;//pa //起始点指向画布中任意点的向量
  float proj1=dot(end,fend); //dot(ba,pa)
  float proj2=dot(start,fstart);
  if(proj1>0.&&proj2>0.){//通过点乘结果>0判断是否同相，过滤掉线段两头超出部分
    
    //求结束点指向画布中任意点的向量与结束点指向起始点的向量的夹角
    float angle=acos(proj1/(length(fend)*length(end)));
    //屏幕上任意点到直线的垂直距离
    float dist=sin(angle)*length(fend);
    return pow(1.-smoothstep(0.,w/2.,dist),6.);
  }else{
    return 0.;
  }
}

float segment2(in vec2 p,in vec2 sp,in vec2 ep,in float w){
  float f=0.;
  vec2 ba=ep-sp;//起始点指向结束点的向量
  vec2 pa=p-sp;//起始点指向画布中任意点的向量
  float proj=clamp(dot(ba,pa)/dot(ba,ba),0.,1.);
  float d=length(proj*ba-pa);
  // f = smoothstep(w,w*0.99,d);
  if(d<=w){
    f=1.;
  }
  return f;
}

float sinLine(in vec2 p,in float sx, in float w){
  // 振幅
  float amplitude =0.3;
  // 角速度（控制波浪周期）
  float angularVelocity =3.;
  // 偏距
  float b =0.;
  // 初相位
  float initialPhase =0.;
  float y = amplitude*sin(angularVelocity*p.x+initialPhase)+b;
  return  smoothstep(y-w/2.,y,p.y)-smoothstep(y,y+w/2.,p.y);
}

float drawPoint(in vec2 p,in vec2 pos, in float s){

float f =0.;

float d = length( p-pos);
f = smoothstep(s,s*0.99,d);

return f ;
}

float drawRect(in vec2 p,in vec2 xy,in float w,in float h){
  float f =0.;

  if(p.x>=xy.x&&p.x<=xy.x+w&&p.y<=xy.y&&p.y>=xy.y-h){
    f = 1.;
  }

  return f;

}

float drawSegenmt(in vec2 p,in vec2 sp, in float len,in float angle,in float w){
  float f =0.;
  float radin =3.1415/180.* angle;
  vec2 ep =sp+ vec2(cos(radin),sin(radin))*len;
  f = segment2(p,sp,ep,w);
  return f;
}

void mainImage(out vec4 fragColor,in vec2 fragCoord){
  
  vec2 p=fixuv(fragCoord);
  vec3 col=GridPanel(p);
  float pct=segment2(p,vec2(0.,0.),vec2(2.,1.),12.*fwidth(p.y));

  float si = sinLine(p,iTime,fwidth(p.x)*20.);
  col=mix(col,vec3(1.,1.,0.),pct);
  col=mix(col,vec3(1.,1.,0.),si);
  col =mix(col,vec3(1.,1.,0.),drawPoint(p,vec2(2.,2.),fwidth(p.x)*10.));
  col=mix(col,vec3(1.,1.,0.),pct);
  col = mix(col,vec3(1.,1.,0.),drawRect(p,vec2(-2.,3.),1.,abs(sin(iTime))*3.));

  col = mix(col,vec3(1.,0.,0.),drawSegenmt(p,vec2(-3.,-3.),2.,(2.*sin(iTime)-1.)*360.,fwidth(p.x)*10.));

  fragColor=vec4(col,1.);
}